package com.xuyoupeng.springlearn.bean;

import com.xuyoupeng.springframework.beans.factory.FactoryBean;
import com.xuyoupeng.springlearn.bean.dao.IUserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuyoupneg
 * @description TODO
 * @date 2024/3/30 13:56
 */
public class ProxyBeanFactory implements FactoryBean<IUserDao> {
    @Override
    public IUserDao getObject() throws Exception {
        InvocationHandler handler = ((proxy, method, args) -> {
            Map<String, String> userMap = new HashMap<>();
            userMap.put("101", "高木");
            userMap.put("102", "西片");
            userMap.put("103", "小千");

            return "代理方法 " + method.getName() + ": " + userMap.get(args[0].toString());
        });
        return (IUserDao) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{IUserDao.class}, handler);
    }

    @Override
    public Class<?> getObjectType() {
        return IUserDao.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
